/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientUser;

import bllInterface.Account;
import bllInterface.BaseChatUser;
import bllInterface.ChatMessage;
import java.rmi.RemoteException;
import java.util.Map;
import model.ClientModel;
import model.chat.ChatMessageModel;
import tbenglish_client.form.privateChat;


public class ChatUserImpl extends BaseChatUser
{
    private boolean isBossRoom = false;
    private Account acc =   null;
    private Map<String, ChatMessageModel> msgModels;
    
    public ChatUserImpl(Account acc, Map<String, ChatMessageModel> models) throws RemoteException
    {
        this.acc = acc;
        this.msgModels = models;
    }
    
    @Override
    public void onHasMessage(ChatMessage msg) throws RemoteException
    {
        if (this.msgModels.containsKey(msg.getSender()))
        {
            this.msgModels.get(msg.getSender()).addMessage(msg);
        }
        else 
        {
            ChatMessageModel chatMsgModel = new ChatMessageModel();
            ClientModel.getInstance().getChatMsgModels().put(msg.getSender(), chatMsgModel);
            
            new privateChat(chatMsgModel, msg.getSender(), msg.getSender()).setVisible(true);
            onHasMessage(msg);
        }
    }

    @Override
    public String getChatID() throws RemoteException
    {
        return this.acc.getUserID();
    }

    @Override
    public String getName() throws RemoteException {
        return acc.getUserName();
    }

    @Override
    public void onHasVoice(String sender, String receiver, byte[] pack) throws RemoteException {
        if (this.msgModels.containsKey(sender))
        {
            this.msgModels.get(sender).addBytes(pack);
        }
    }
    
    @Override
    public void onHasKey( String sender ) throws RemoteException{
        if (this.msgModels.containsKey(sender))
        {
            this.msgModels.get(sender).acceptKey();
            setBoss( sender, true );
        }
    }

    @Override
    public boolean isBoss( String receiver ) throws RemoteException {
        if( msgModels.containsKey(receiver) )
        {
            return msgModels.get( receiver ).getIsBoss();
        }
        return false;
    }

    @Override
    public void setBoss( String receiver, boolean isBoss) throws RemoteException {
        if( msgModels.containsKey(receiver) )
        {
            msgModels.get( receiver ).setIsBoss(isBoss);
        }
    }
}
